package org.seven.jrdp.web.mgt.sys;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.seven.jrdp.biz.sys.model.SysRole;
import org.seven.jrdp.biz.sys.service.SysRoleService;
import org.seven.jrdp.commons.model.Result;
import org.seven.jrdp.commons.model.TreeNode;
import org.seven.jrdp.commons.shiro.model.User;
import org.seven.jrdp.commons.util.ShiroUtils;

@RestController
@RequestMapping("/mgt/sys/role")
public class SysRoleController {
	@Autowired
	private SysRoleService sysRoleService;

	@GetMapping("/{view}")
	public ModelAndView view(@PathVariable String view) {
		return new ModelAndView("mgt/sys/role/role_" + view);
	}

	@PostMapping("/find")
	public List<SysRole> find() {
		List<SysRole> list = sysRoleService.find();
		return list;
	}

	@RequestMapping("/get")
	public SysRole get(String id) {
		SysRole sysRole = sysRoleService.get(id);
		return sysRole;
	}

	@PostMapping("/insert")
	public Result insert(SysRole sysRole) {
		sysRoleService.insert(sysRole);
		return Result.SUCCESS;
	}

	@PostMapping("/update")
	public Result update(SysRole sysRole) {
		sysRoleService.update(sysRole);
		return Result.SUCCESS;
	}

	@PostMapping("/delete")
	public Result delete(String id) {
		sysRoleService.delete(id);
		return Result.SUCCESS;
	}

	@PostMapping("/res")
	public Result saveRes(String id, String[] resIds) {
		sysRoleService.saveRes(id, resIds);
		return Result.SUCCESS;
	}

	@PostMapping("/role")
	public Result saveRole(String id, String[] roleIds) {
		sysRoleService.saveRole(id, roleIds);
		return Result.SUCCESS;
	}

	@RequestMapping("/tree")
	public List<TreeNode> tree() {
		List<TreeNode> list = sysRoleService.findTree();
		return list;
	}

	@RequestMapping("/tree/user")
	public List<TreeNode> user() {
		List<TreeNode> list = null;
		User user = ShiroUtils.getUser();
		if (user.isAdmin()) {
			list = sysRoleService.findTree();
		} else {
			list = sysRoleService.findTreeByUserId(user.getId());
		}
		return list;
	}

	@RequestMapping("/res/tree/{id}")
	public List<TreeNode> res(@PathVariable String id) {
		List<TreeNode> list = sysRoleService.findResTreeById(id);
		return list;
	}

	@RequestMapping("/role/tree/{id}")
	public List<TreeNode> role(@PathVariable String id) {
		List<TreeNode> list = sysRoleService.findRoleTreeById(id);
		return list;
	}
}